//
//  ForgetPwdViewController.m
//  text
//
//  Created by 郑州动力无限科技 on 2017/3/8.
//  Copyright © 2017年 郑州动力无限科技. All rights reserved.
//

#import "ForgetPwdViewController.h"

@interface ForgetPwdViewController ()<UITextFieldDelegate>
{
    NSTimer * _timer ;
    NSInteger  _secondsCountDown;
    NSString * _codeStr ;
}
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet UITextField *morePsdTF;
- (IBAction)commitBtn:(id)sender;
- (IBAction)getCode:(id)sender;


@end

@implementation ForgetPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self initControl];
}
#pragma mark - 子类继承
-(CMNavType)getNavType {
    return CMNavTypeNoRightItem;
}
-(void)initControl{
    _passwordTF.delegate = self;
    _morePsdTF.delegate = self;
    _codeTF.delegate = self;

    _phoneTF.delegate =self;

    [_phoneTF addTarget:self action:@selector(limitTextlength:) forControlEvents:UIControlEventEditingChanged];

    [_codeTF addTarget:self action:@selector(codeLimitTextlength:) forControlEvents:UIControlEventEditingChanged];
    [_codeBtn setUserInteractionEnabled:NO];
    _codeBtn.layer.cornerRadius = 2.0f;
    _codeBtn.layer.masksToBounds = YES;
    _nextBtn.layer.cornerRadius = 2.0f;
    _nextBtn.layer.masksToBounds = YES;
    [_nextBtn setUserInteractionEnabled:NO];
    [_nextBtn setBackgroundColor:[UIColor lightGrayColor]];
    
}
#pragma mark -  userTouch
- (IBAction)commitBtn:(id)sender {
    if ([FXTPublicTools isBlankString:_phoneTF.text]==YES) {
        [DisplayHelper displayWarningAlert:@"请输入手机号"];
    }else if ([FXTPublicTools isBlankString:_passwordTF.text]==YES){
        [DisplayHelper displayWarningAlert:@"请输入密码" ];
    }else{
        if ([self.codeTF.text isEqualToString:_codeStr]) {
            [self registerRequest];
        }else{
            [DisplayHelper displayWarningAlert:@"验证码不正确，请重新输入"];
        }
    }
}
#pragma mark -  Request
-(void)registerRequest{
    FXTHttpRequestModel * requestModel = [[FXTHttpRequestModel alloc] init];
    NSMutableDictionary * paramDic = [NSMutableDictionary dictionary];
    requestModel.appendUrl = kLogin_FindUserPassword;
    [paramDic setValue:_passwordTF.text forKey:@"password"];
    [paramDic setValue:_phoneTF.text forKey:@"telephone"];

    requestModel.paramDic = paramDic;
    WS(ws);
    [[DisplayHelper shareDisplayHelper]showLoading:self.view];
    requestModel.callback = ^(FXTHttpResponseModel * result, NSError *error){
        [[DisplayHelper shareDisplayHelper]hideLoading:ws.view];
        if (result.state ==FXTReponseCodeState_Success) {
            [DisplayHelper displaySuccessAlert:result.alertMsg];
            if (ws.isForget == YES) {
            [ws.navigationController popViewControllerAnimated:YES];
            }else{
            [ws loginOutRequest];   
            }
        }else{
            [DisplayHelper displayWarningAlert:result.alertMsg];
        }
    };
    [[FXTHttpSessionManager sharedHttpSessionManager] sendHttpRequestParam:requestModel];
}
-(void)loginOutRequest{
    
    FXTHttpRequestModel * requestModel = [[FXTHttpRequestModel alloc] init];
    requestModel.type = FXTHttpType_GET;
//    requestModel.appendUrl = kLogin_Logout;
    requestModel.callback = ^(FXTHttpResponseModel * result, NSError *error){
        if (result.state ==FXTReponseCodeState_Success) {
//            [SMUserTools removeAllWechatAndUserDatas];
            [[FXTPublicTools getAppDelegate]initRootViewController];
        }else{
            [DisplayHelper displayWarningAlert:result.alertMsg];
        }
    };
    [[FXTHttpSessionManager sharedHttpSessionManager] sendHttpRequestParam:requestModel];
}

#pragma mark    获取验证码改变顶上的数字
-(void)getCaptcha{
    FXTHttpRequestModel * requestModel = [[FXTHttpRequestModel alloc] init];
    requestModel.type = FXTHttpType_POST;
    NSMutableDictionary * paramDic = [NSMutableDictionary dictionary];
    requestModel.appendUrl = kLogin_RegisterPhoneCode;
    [paramDic setValue:@1 forKey:@"isregister"];
    [paramDic setValue:_phoneTF.text forKey:@"telephone"];
    requestModel.paramDic = paramDic;
    WS(ws);
    [[DisplayHelper shareDisplayHelper]showLoading:self.view];
    requestModel.callback = ^(FXTHttpResponseModel * result, NSError *error){
        if (result.state ==FXTReponseCodeState_Success) {
            [DisplayHelper displaySuccessAlert:result.alertMsg];
            _codeStr = result.data;
        }else{
            [DisplayHelper displayWarningAlert:result.alertMsg];
            [_codeBtn setTitle: [NSString stringWithFormat:@"重新获取"] forState:UIControlStateNormal];
            _codeBtn.userInteractionEnabled = YES;
            [_timer invalidate];
        }
        [[DisplayHelper shareDisplayHelper]hideLoading:ws.view];
    };
    [[FXTHttpSessionManager sharedHttpSessionManager] sendHttpRequestParam:requestModel];
}
#pragma mark - 获取验证码按钮点击事件

- (IBAction)getCode:(id)sender {
    if ([FXTPublicTools isBlankString:_phoneTF.text]==YES) {
        [DisplayHelper displayWarningAlert:@"请输入手机号"];
    }else{
        UIButton * btn  = (UIButton*)sender;
        [self getCaptcha];
        btn.userInteractionEnabled= NO;
        _secondsCountDown = 60;
        _timer  = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeNumber) userInfo:nil repeats:YES];
    }
}
// 改变数字  计时
- (void)changeNumber
{
    _secondsCountDown --;
    [_codeBtn setTitle: [NSString stringWithFormat:@"%lds",(long)_secondsCountDown] forState:UIControlStateNormal];
    if (_secondsCountDown == 0) {
        [_codeBtn setTitle: [NSString stringWithFormat:@"重新获取"] forState:UIControlStateNormal];
        _codeBtn.userInteractionEnabled = YES;
        [_timer invalidate];
    }
}

#pragma mark           UITextFildDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [_phoneTF resignFirstResponder];
    [_codeTF resignFirstResponder];
    [_passwordTF resignFirstResponder];
    [_morePsdTF resignFirstResponder];
    return YES;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

#pragma mark 手机号码输入长度限制
- (void)limitTextlength:(UITextField *)textField
{
    if ([textField.text length]>10)
    {
        textField.text=[textField.text substringToIndex:11];
        [_codeBtn setUserInteractionEnabled:YES];
        
        if ([_codeTF.text length]>3)
        {
            [_nextBtn setUserInteractionEnabled:YES];
            [_nextBtn setBackgroundColor:NavCOLOR];
            
        }
    } else {
        [_codeBtn setUserInteractionEnabled:NO];
        [_nextBtn setUserInteractionEnabled:NO];
        [_nextBtn setBackgroundColor:[UIColor lightGrayColor]];
    }
}
#pragma mark 验证码输入长度判断
- (void)codeLimitTextlength:(UITextField *)textField
{
    if ([textField.text length]>3)
    {
        if ([_phoneTF.text length] > 10) {
            [_nextBtn setUserInteractionEnabled:YES];
            [_nextBtn setBackgroundColor:NavCOLOR];
        }
        
    } else {
        [_nextBtn setUserInteractionEnabled:NO];
        [_nextBtn setBackgroundColor:[UIColor lightGrayColor]];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
